package com.augurit.gzcgwsp.state.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;
import com.augurit.common.syscode.service.ISysCodeService;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.state.service.IYwSummaryStateService;
import com.augurit.gzcgwsp.state.service.IYwYnpfStateService;
import com.augurit.gzcgwsp.state.util.RequestUtil;
import com.augurit.gzcgwsp.ywcmn.entity.BaseDistrict;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Component("ywYnPfStateAtcion")
@ParentPackage("struts-default")
@Namespace("/state")
@Scope("prototype")
@Action(value="ywynpf-state",results={
	 	@Result(name = "success", location = "/gzcgwsp/state/ynpfxn/listynpfxnState.jsp"),
		@Result(name = "toDatas", location = "/gzcgwsp/state/ynpfxn/listynpf.jsp"),
		@Result(name = "toJmProject", location = "/gzcgwsp/state/ynpfxn/listJmProject.jsp"),
		@Result(name = "listpfgd", location = "/gzcgwsp/state/ynpfxn/listpfgd.jsp"),
		@Result(name = "listpfgdbyattribute", location = "/gzcgwsp/state/ynpfxn/listpfgdbyattribute.jsp"),
		@Result(name = "toQueryPage", location = "/gzcgwsp/state/ynpfxn/querypage.jsp")
	}
)
/**
 * 余泥排放减免查询Action
 */
public class YwYnPfStateAtcion extends ActionSupport implements ModelDriven {
	@Autowired
	private IYwYnpfStateService ywYnpfStateService;//余泥排放统计
	@Autowired
	private ISysCodeService sysCodeService;
	@Autowired
	private IYwSummaryStateService ywSummaryStateService;
	private Page page ;

	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	@Override
	public String execute() throws Exception {
			  return SUCCESS;
	}
	

	public String toDatas() throws Exception {
			  return "toDatas";
	}
	/**
	 * 统计排放列表
	 * @return
	 */
	public String getDatas(){
	    Map map = RequestUtil.getMapByRequest(getRequest());
	    if(page==null){
	  		  page = new Page(15);
	  	 }
	    ExtUtils.initPageFromExtGridParam(getRequest(), page);
	    page=this.ywYnpfStateService.searchPfData(page,map);
	  	
	  	renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}
	
	/**
	 * 统计排放列表2
	 * @return
	 */
	public String toDatas2(){
		String gjz = getRequest().getParameter("gjz");
		String flag = getRequest().getParameter("flag");
		Enumeration names = getRequest().getParameterNames();
		while(names.hasMoreElements()){
		    String name=(String)names.nextElement();
		    String value=getRequest().getParameter(name);
		    getRequest().setAttribute(name, value);
		}
		System.out.println("flag = "+flag);
		String [] xsnrArr = getRequest().getParameterValues("xsnr");//选中的checkbox
		if(xsnrArr == null && "1".equals(flag)){
			xsnrArr = (String [])getRequest().getSession().getAttribute("xsnr");
		}
		List<String> keyList = new ArrayList<String>();
		List<String> valueList = new ArrayList<String>();
		String xsnrs = "";//所有选中name="xsnr"的input对象组成的字符串
		if(xsnrArr != null){
			for(int i=0;i<xsnrArr.length;i++){
				String xsnrStr = xsnrArr[i];
				xsnrs = xsnrs + "+++" + xsnrStr; 
//				System.out.println("xsnrStr = "+xsnrStr);
				String key = xsnrStr.split("==")[0];
				String value = xsnrStr.split("==")[1];
//				System.out.println("key = "+key);
//				System.out.println("value = "+value);
				keyList.add(key);
				valueList.add(value);
			}
			getRequest().setAttribute("keyList", keyList);
			getRequest().setAttribute("valueList", valueList);
			getRequest().getSession().setAttribute("xsnr", xsnrArr);
			getRequest().getSession().setAttribute("xsnrs", xsnrs);
		}
		if(StringUtils.isNotBlank(gjz)){
			this.getRequest().setAttribute("gjz", gjz);
		}
		//获取参数时，是否要编码标识，当点列表上面的区查询时，url有中文，获取参数时要编码
		String encoding = getRequest().getParameter("encoding");
		Map map = null;
		if(StringUtils.isNotBlank(encoding)){
			map = RequestUtil.getMapByRequestAndEncoding(getRequest());
		}else{
			map = RequestUtil.getMapByRequest(getRequest());
		}
		
	    if(page==null){
	  		  page = new Page(10);
	  	 }
	    ExtUtils.initPageFromExtGridParam(getRequest(), page);
	    page=this.ywYnpfStateService.search(page,map);
	    if(xsnrArr != null && xsnrArr.length>0){
	    	return "listpfgdbyattribute";
	    }
		return "listpfgd";
	}
	
	/**
	 * 打开高级查询页面
	 * @return
	 */
	public String toQueryPage(){
		 List<SysCodeForm> gclb = sysCodeService.getItems("YW_GCLB");
	     getRequest().setAttribute("gclb", gclb);
	     List<BaseDistrict> list=this.ywSummaryStateService.getArea();
		 List<SysCodeForm> sysCodeFormList = new ArrayList<SysCodeForm>();
    	 if(list!=null && list.size()>0){
    		 for (int i=0;i<list.size();i++) {
    			BaseDistrict area=list.get(i);
    			SysCodeForm sysCodeForm = new SysCodeForm();
    			sysCodeForm.setItemCode(area.getDistrictName());
    			sysCodeForm.setItemName(area.getDistrictName());
    			sysCodeFormList.add(sysCodeForm);
    		 }
    	 }
    	 getRequest().setAttribute("area", sysCodeFormList);
	     return "toQueryPage";
	}
	
	/**
	 * 统计工程列表
	 * @return
	 */
	public String getProjectDatas(){
	    Map map = RequestUtil.getMapByRequest(getRequest());
	    if(page==null){
	  		  page = new Page(15);
	  	 }
	    ExtUtils.initPageFromExtGridParam(getRequest(), page);
	    page=this.ywYnpfStateService.search(page,map);
	  	
	  	renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}
	
	/**
	 * 到减免工程查询页面
	 * @return
	 */
	public String toJmProject(){
		return "toJmProject";
	}
	
	/**
	 * 统计工程列表
	 * @return
	 */
	public String getJmProjectDatas(){
	    Map map = RequestUtil.getMapByRequest(getRequest());
	    if(page==null){
	  		  page = new Page(15);
	  	 }
	    ExtUtils.initPageFromExtGridParam(getRequest(), page);
	    page=this.ywYnpfStateService.searchJmProjectDatas(page,map);
	  	
	  	renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}
	
	
	/**
	 * 导出数据
	 * @return
	 * @throws IOException 
	 */
	public String exportProjectData() throws IOException{
		Map map=RequestUtil.getMapByRequest(getRequest());
    	String contextpath=getRequest().getRealPath("");
		map.put("contextpath", contextpath);
    	String path= this.ywYnpfStateService.exportProjectData(map);
		if(StringUtils.isNotBlank(path)){
			getResponse().getWriter().print(path);
		}
		return null;
	}
	
	/**
	 * 导出数据
	 * @return
	 * @throws IOException 
	 */
	public String exportData() throws IOException{
		Map map=RequestUtil.getMapByRequest(getRequest());
    	String contextpath=getRequest().getRealPath("");
		map.put("contextpath", contextpath);
    	String path= this.ywYnpfStateService.exportState(map);
		if(StringUtils.isNotBlank(path)){
			getResponse().getWriter().print(path);
		}
		return null;
	}
	
	/**
	 * 取得HttpRequest的简化方法.
	 */
	private static HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 取得HttpResponse的简化方法.
	 */
	private static HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	public Object getModel() {
		return null;
	}
	/**
	 * 直接输出文本.
	 * @see #render(String, String, String...)
	 */
	public static void renderText(final String text, final String... headers) {
		Struts2Utils.renderText(text, headers);
	}
}
